package team.hyznrj.studentsys.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import team.hyznrj.studentsys.dao.mapper.QualityTypeMapper;
import team.hyznrj.studentsys.dao.repository.AcademicYearsRepository;
import team.hyznrj.studentsys.dao.repository.CurriculumNatureRepository;
import team.hyznrj.studentsys.entity.AcademicYears;
import team.hyznrj.studentsys.entity.CurriculumNature;
import team.hyznrj.studentsys.entity.QualityType;
import team.hyznrj.studentsys.exception.DataIllegalException;
import team.hyznrj.studentsys.exception.DataNullException;
import team.hyznrj.studentsys.form.CourseAchievementForm;

/**
 * 数据库静态数据提取类
 * 
 * @author 郑泽锋
 * @date 2018年4月26日
 */
@Service
public class StaticDatabasesValueCache {
	/**
	 * 学年格式标准
	 */
	public final String YEARS_PATTERN = "^[123456789][0-9][0-9][0-9]-[123456789][0-9][0-9][0-9]$";
	/**
	 * 获取学年字段
	 */
	@Autowired
	private AcademicYearsRepository academicYearsRepository;
	/**
	 * 操行分类型MAPPER
	 */
	@Autowired
	private QualityTypeMapper typeMapper;
	@Autowired
	private CurriculumNatureRepository curriculumNatureRepository;
	/**
	 * 学年字段映射缓存(id->value)
	 */
	private final Map<Integer, String> YEARS = new ConcurrentHashMap<>();
	/**
	 * 学年字段映射缓存(value->id)
	 */
	private final Map<String, Integer> YEARS_BACK = new ConcurrentHashMap<>();
	/**
	 * 操行分类型字段映射缓存(id->value)
	 */
	private final Map<String, String> QUALITY_TYPE = new ConcurrentHashMap<>();
	/**
	 * 操行分类型字段映射缓存(value->id)
	 */
	private final Map<String, String> QUALITY_TYPE_BACK = new ConcurrentHashMap<>();

	List<CurriculumNature> curriculumNatureList = null;

	/***
	 * 获取学科类型
	 * 
	 * @param index
	 * @return
	 */
	public String getCurriculumNatureValueById(final Integer index) {
		if (curriculumNatureList == null) {
			curriculumNatureList = curriculumNatureRepository.findAll();
		}
		for (CurriculumNature n : curriculumNatureList) {
			if (n.getId() == index) {
				return n.getCurriculumNatureName();
			}
		}
		return null;
	}

	/**
	 * 判断是否合法年份格式
	 * 
	 * @param year
	 * @return
	 */
	public boolean legalYearsPattern(final String year) {
		if (StringUtils.isEmpty(year)) {
			throw new DataNullException("学年不合法");
		}
		/**
		 * 条件: 1.XXXX-XXXX格式 2.年份相差1
		 */
		if (year.matches(YEARS_PATTERN)) {
			String[] split = year.split("-");
			if (split.length == 2) {
				if ((Integer.valueOf(split[1]) - Integer.valueOf(split[0])) == 1) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 取学年字段 通过id
	 * 
	 * @param id
	 * @return
	 */
	public String getYearById(int id) {
		if (!YEARS.containsKey(id)) {
			/**
			 * 如果找不到id,说明需要更新一下缓存
			 */
			List<AcademicYears> findAll = academicYearsRepository.findAll();
			for (int i = 0; i < findAll.size(); i++) {
				AcademicYears academicYears = findAll.get(i);
				YEARS.put(academicYears.getId(), academicYears.getAcademicYear());
				YEARS_BACK.put(academicYears.getAcademicYear(), academicYears.getId());
			}
		}
		return YEARS.get(id);
	}

	/**
	 * 取学年字段 通过字段
	 * 
	 * @param id
	 * @return
	 */
	public Integer getYearByValue(String value) {
		if (!legalYearsPattern(value)) {
			throw new DataIllegalException("学年不合法");
		}
		if (!YEARS_BACK.containsKey(value)) {
			/**
			 * 如果找不到id,说明需要更新一下缓存
			 */
			List<AcademicYears> findAll = academicYearsRepository.findAll();
			for (int i = 0; i < findAll.size(); i++) {
				AcademicYears academicYears = findAll.get(i);
				YEARS.put(academicYears.getId(), academicYears.getAcademicYear());
				YEARS_BACK.put(academicYears.getAcademicYear(), academicYears.getId());
			}
		}
		if (!YEARS_BACK.containsKey(value)) {
			AcademicYears academicYears = new AcademicYears(value);
			AcademicYears year = academicYearsRepository.save(academicYears);
			Integer academicYearId = year.getId();
			YEARS_BACK.put(value, academicYearId);
		}
		return YEARS_BACK.get(value);
	}

	/**
	 * 取项目类型字段
	 * 
	 * @param id
	 * @return
	 */
	public String getQualitiyTypeById(String id) {
		if (StringUtils.isEmpty(id)) {
			throw new DataNullException("操行分类型值不合法");
		}
		if (!QUALITY_TYPE.containsKey(id)) {
			/**
			 * 如果找不到id,说明需要更新一下缓存
			 */
			List<QualityType> selectByExample = typeMapper.selectByExample(null);
			for (int i = 0; i < selectByExample.size(); i++) {
				QualityType qualityType = selectByExample.get(i);
				QUALITY_TYPE.put(qualityType.getId(), qualityType.getValue());
				QUALITY_TYPE_BACK.put(qualityType.getValue(), qualityType.getId());
			}
		}
		return QUALITY_TYPE.get(id);
	}

	public String getQualitiyTypeByValue(String value) {
		if (StringUtils.isEmpty(value)) {
			throw new DataNullException("操行分类型值不合法");
		}
		if (!QUALITY_TYPE_BACK.containsKey(value)) {
			/**
			 * 如果找不到value,说明需要更新一下缓存
			 */
			List<QualityType> selectByExample = typeMapper.selectByExample(null);
			for (int i = 0; i < selectByExample.size(); i++) {
				QualityType qualityType = selectByExample.get(i);
				QUALITY_TYPE.put(qualityType.getId(), qualityType.getValue());
				QUALITY_TYPE_BACK.put(qualityType.getValue(), qualityType.getId());
			}
		}
		return QUALITY_TYPE_BACK.get(value);
	}

	/**
	 * 返回操行分类型
	 * 
	 * @return
	 */
	public List<QualityType> getQualityTypes() {
		return typeMapper.selectByExample(null);
	}
	/**
	 * 获取academicYearId(这个方法必然有一个返回值)
	 * @param academicYearsList
	 * @param year
	 * @return
	 */
	public Integer getAcademicYearId(String year) {
		if(!legalYearsPattern(year)){
			return null;
		}
		List<AcademicYears> academicYearsList = academicYearsRepository.findAll();
		Integer academicYearId = null;
		for (AcademicYears academicYear : academicYearsList) {
			if (academicYear.getAcademicYear().equals(year)) {
				academicYearId = academicYear.getId();
				break;
			}
		}
		// 若学年表不存在则添加至学年表
		if (academicYearId == null) {
			AcademicYears academicYears = new AcademicYears(year);
			AcademicYears result = academicYearsRepository.save(academicYears);
			academicYearId = result.getId();
		}
		return academicYearId;
	}
}
